package org.lex.ftp.ui.bookmark;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import org.lex.awt.ComponentUtilities;
import org.lex.ftp.util.bookmark.Bookmark;
import org.lex.ftp.util.bookmark.BookmarkUtils;
import org.lex.ftp.util.bookmark.Site;
import org.lex.net.ftp.FTPURL;
import org.lex.swing.ContainerFactory;
import org.lex.swing.widgets.EnhancedDialog;


public class BookmarkDialog extends EnhancedDialog {
	private JPanel pane = null;
	private JSplitPane splitPane = null;
	private BookmarkTree bookmarkTree = null;
	private BookmarkEditPane editPane = null;
	// control buttons
	private JPanel buttonsPane = null;
	private JPanel leftButtonsPane = null;
	private JPanel rightButtonsPane = null;
	private JButton newSiteButton = null;
	private JButton newGroupButton = null;
	private JButton deleteButton = null;
	private JButton closeButton = null;

	public static void main(String[] args) {
		new BookmarkDialog(null).setVisible(true);
	}

	public BookmarkDialog(Window owner) {
		super(owner);
		initialize();
		this.setLocationRelativeTo(owner);
	}

	public void cancel() {
		bookmarkTree.clearSelection();
		try {
			BookmarkUtils.storeBookmark(bookmarkTree.getRootBookmark());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		dispose();
	}

	private void newSiteAction() {
		String name = this.getInput("New Site", "Enter Site Name:");
		if (null == name || name.isEmpty())
			return;
		this.bookmarkTree.addSite(new Site(name, FTPURL.FAKE_URL));
	}

	private void newGroupAction() {
		String name = this.getInput("New Group", "Enter Group Name:");
		if (null == name || name.isEmpty())
			return;
		this.bookmarkTree.addGroup(new Bookmark(name));
	}

	private String getInput(String title, String message) {
		return JOptionPane.showInputDialog(this, message, title, JOptionPane.QUESTION_MESSAGE);
	}

	private void hookActionHandlers() {
		// tree selection
		this.bookmarkTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
			private BookmarkTreeNode lastNode = null;

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				// save changes
				if (true == editPane.saveChanges() && null != lastNode)
					bookmarkTree.getModel().nodeChanged(lastNode);
				// show current selection
				BookmarkTreeNode node = bookmarkTree.getSelectBookmarkNode();
				if (null == node) {
					// nothing is selected
					editPane.reset();
					ComponentUtilities.setEnabled(leftButtonsPane, false);
					return;
				} else {
					lastNode = node;
					ComponentUtilities.setEnabled(leftButtonsPane, true);
					if (node.isSite())
						editPane.setSite(node.getSite());
					else if (node.isFolder())
						editPane.reset();
				}
			}
		});
		// tree edit
		this.bookmarkTree.setEditable(true);
		this.bookmarkTree.setCellEditor(new DefaultTreeCellEditor(bookmarkTree, (DefaultTreeCellRenderer) bookmarkTree.getCellRenderer()) {
			// @Override
			// public boolean isCellEditable( EventObject event )
			// {
			// return true;
			// }
		});
		// add site
		this.getNewSiteButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				newSiteAction();
			}
		});
		// add group
		this.getNewGroupButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				newGroupAction();
			}
		});
		// delete selected
		this.getDeleteButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				bookmarkTree.deleteSelected();
			}
		});
		// close
		this.getCloseButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancel();
			}
		});
	}

	private void initialize() {
		this.setSize(500, 420);
		this.setContentPane(getPane());
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Bookmark");
		this.hookActionHandlers();
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getSplitPane(), BorderLayout.CENTER);
			pane.add(getButtonsPane(), BorderLayout.SOUTH);
			// pane.add( Box.createVerticalStrut( 5 ), BorderLayout.NORTH );
		}
		return pane;
	}

	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = ContainerFactory.createBorderlessSplitPane(5);
			splitPane.setResizeWeight(0.25D);
			splitPane.setLeftComponent(new JScrollPane(this.getBookmarkTree()));
			splitPane.setRightComponent(getEditPane());
		}
		return splitPane;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			buttonsPane = new JPanel();
			buttonsPane.setLayout(new BorderLayout());
			buttonsPane.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0));
			buttonsPane.add(getLeftButtonsPane(), BorderLayout.WEST);
			buttonsPane.add(getRightButtonsPane(), BorderLayout.EAST);
		}
		return buttonsPane;
	}

	private JPanel getLeftButtonsPane() {
		if (leftButtonsPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			flowLayout.setVgap(0);
			flowLayout.setHgap(12);
			leftButtonsPane = new JPanel();
			leftButtonsPane.setLayout(flowLayout);
			leftButtonsPane.add(getNewSiteButton(), null);
			leftButtonsPane.add(getNewGroupButton(), null);
			leftButtonsPane.add(getDeleteButton(), null);
		}
		return leftButtonsPane;
	}

	private JPanel getRightButtonsPane() {
		if (rightButtonsPane == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setAlignment(FlowLayout.RIGHT);
			flowLayout1.setVgap(0);
			flowLayout1.setHgap(12);
			rightButtonsPane = new JPanel();
			rightButtonsPane.setLayout(flowLayout1);
			rightButtonsPane.add(getCloseButton(), null);
		}
		return rightButtonsPane;
	}

	private JButton getNewSiteButton() {
		if (newSiteButton == null) {
			newSiteButton = new JButton();
			newSiteButton.setText("New Site");
		}
		return newSiteButton;
	}

	private JButton getNewGroupButton() {
		if (newGroupButton == null) {
			newGroupButton = new JButton();
			newGroupButton.setText("New Group");
		}
		return newGroupButton;
	}

	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("Delete");
		}
		return deleteButton;
	}

	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close");
		}
		return closeButton;
	}

	private BookmarkTree getBookmarkTree() {
		if (null == bookmarkTree) {
			Bookmark root;
			try {
				root = BookmarkUtils.loadBookmark();
			} catch (Exception e) {
				e.printStackTrace();
				root = new Bookmark("FTP Bookmark");
			}
			bookmarkTree = new BookmarkTree(root);
			bookmarkTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		}
		return bookmarkTree;
	}

	private BookmarkEditPane getEditPane() {
		if (editPane == null) {
			editPane = new BookmarkEditPane();
			editPane.setBorder(new EmptyBorder(6, 6, 11, 11));
		}
		return editPane;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
